# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=ogre3d
pkgbase=mingw-w64-${_realname}-hg
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-hg"
_rev=r10230.62834e3ac215
_base_ver=2.1.0
pkgver=${_base_ver}.${_rev}
pkgrel=1
pkgdesc="A cross-platform 3D game engine (mingw-w64)"
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
arch=('any')
url="http://www.ogre3d.org/"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "${MINGW_PACKAGE_PREFIX}-cmake" "mercurial")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
        "${MINGW_PACKAGE_PREFIX}-cppunit"
        "${MINGW_PACKAGE_PREFIX}-FreeImage"
        "${MINGW_PACKAGE_PREFIX}-freetype"
        "${MINGW_PACKAGE_PREFIX}-glsl-optimizer"
        "${MINGW_PACKAGE_PREFIX}-hlsl2glsl"
        "${MINGW_PACKAGE_PREFIX}-intel-tbb"
        #"${MINGW_PACKAGE_PREFIX}-nvidia-cg-toolkit"
        "${MINGW_PACKAGE_PREFIX}-ois-git"
        "${MINGW_PACKAGE_PREFIX}-poco"
        "${MINGW_PACKAGE_PREFIX}-SDL2"
        "${MINGW_PACKAGE_PREFIX}-tinyxml"
        "${MINGW_PACKAGE_PREFIX}-zlib"
        "${MINGW_PACKAGE_PREFIX}-zziplib"
        )
options=('staticlibs' '!strip') # '!buildflags'
source=("${_realname}-${pkgver}.tar.xz"{,.sig}
        002-endlessworld-fix-cast.patch
        003-freeimage-link-with-shared.patch
        004-use-mingw-w64-directx.patch
        005-mingw-w64-redeclarations.patch
        007-fix-_WIN32-defines2.patch
        010-missing-include.patch
        011-fix-typo.patch
        012-add-link-library.patch
        013-mingw-stringconverter.patch
        014-custom-install.patch)
md5sums=('98e375459a3ac26fb4d32306b4c7b46e'
         'SKIP'
         '3c00c5584a8edcb4dc07a57697a73d59'
         '166486655cd358be175f3b32b63e0ca5'
         '51cf86561d243f3564758c6255b1b0a5'
         '57ca4bf629fec2df4aeccb617ad187ac'
         '9af326a7f9c7863e25ea265de4a2a223'
         '2ada87678777366fa9cdcb5e7d12b650'
         'f7d17b37f835d20a4fd6262d77d9b00a'
         '769784029b7059ef89da7ec0186da9cd'
         'abc014b040b651d762d519d7079f19b7'
         '10768fba9873f2bfe923e9a7e416c964')

# source PKGBUILD && mksource
mksource() {
  [[ -x /usr/bin/hg ]] || (echo "hg not found. Install mercurial." && return 1)

  __hgroot='https://bitbucket.org/sinbad'
  __hgrepo='ogre'
  __hgbranch='default'
  __hgrev="v2-1"

  hg clone -u ${__hgrev} "${__hgroot}/${__hgrepo}/" ${__hgrepo}
  #hg clone -b ${__hgbranch} "${__hgroot}${__hgrepo}#${__hgrev}" ${__hgrepo}

  pushd ${__hgrepo}
    if (( $(hg id -n) < $(hg id -nr ${__hgbranch}) )); then
      printf 'You are not building the latest revision!\n'
      printf "Consider updating __hgrev to $(hg id -r ${__hgbranch}).\n"
    fi
    __revis="r$(hg identify -n).$(hg identify -i)"
  popd

  mv ${__hgrepo} ${_realname}-${_base_ver}.${__revis}
  find ${_realname}-${_base_ver}.${__revis} -depth -type d -name .hg -exec rm -rf {} \;
  rm ${_realname}-${_base_ver}.${__revis}/{.hgignore,.hgtags}
  tar -cJf ${_realname}-${_base_ver}.${__revis}.tar.xz ${_realname}-${_base_ver}.${__revis}/*
  rm -r ${_realname}-${_base_ver}.${__revis}

  gpg --detach-sign ${_realname}-${_base_ver}.${__revis}.tar.xz

  #scp ${pkgname}-${_base_ver}.tar.xz nym:/srv/ftp/other/vim/
  #scp ${pkgname}-${_base_ver}.tar.xz.sig nym:/srv/ftp/other/vim/
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p0 -i ${srcdir}/002-endlessworld-fix-cast.patch
  patch -p1 -i ${srcdir}/003-freeimage-link-with-shared.patch
  patch -p1 -i ${srcdir}/004-use-mingw-w64-directx.patch
  patch -p1 -i ${srcdir}/005-mingw-w64-redeclarations.patch
  patch -p1 -i ${srcdir}/007-fix-_WIN32-defines2.patch
  patch -p1 -i ${srcdir}/010-missing-include.patch
  patch -p1 -i ${srcdir}/011-fix-typo.patch
  patch -p1 -i ${srcdir}/012-add-link-library.patch
  patch -p1 -i ${srcdir}/013-mingw-stringconverter.patch
  patch -p1 -i ${srcdir}/014-custom-install.patch
}

build() {
  local -a extra_config

  if check_option "debug" "y"; then
    extra_config+=( -DCMAKE_BUILD_TYPE=Debug )
  else
    extra_config+=( -DCMAKE_BUILD_TYPE=Release )
  fi

  CFLAGS+=" -O3 -fno-devirtualize"
  CXXFLAGS+=" -O3 -fno-devirtualize"
  export DXSDK_DIR=${MINGW_PREFIX}/${MINGW_CHOST}
  mkdir build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DOGRE_DEPENDENCIES_DIR=${MINGW_PREFIX} \
    -DOGRE_INSTALL_TOOLS=ON \
    -DOGRE_COPY_DEPENDENCIES=OFF \
    -DOGRE_INSTALL_DEPENDENCIES=OFF \
    -DOGRE_BUILD_RENDERSYSTEM_D3D9=ON \
    -DOGRE_BUILD_RENDERSYSTEM_D3D11=ON \
    -DFREETYPE_FT2BUILD_INCLUDE_DIR=${MINGW_PREFIX}/include/freetype2 \
    "${extra_config[@]}" \
    ../${_realname}-${pkgver}

  make
}

package() {
  local -a _buildtype

  if check_option "debug" "y"; then
    _buildtype=Debug
  else
    _buildtype=Release
  fi
  
  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 DESTDIR=${pkgdir} install
}
